package com.ds.controller.restful;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ds.orm.model.User;
import com.ds.service.UserService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.ds.core.base.BaseController;
import com.ds.core.support.Assert;
import com.ds.core.util.WebUtil;

/**
 * <p>
 * 用户管理  前端控制器
 * </p>
 *
 * @author Du
 * @since 2017-11-19
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户管理接口", description = "用户管理接口")
public class UserController extends BaseController<User> {
	public String getService() {
		return "userService";
	}

	@RequiresPermissions("user.read")
	@PutMapping(value = "/read/list")
	@ApiOperation(value = "查询用户管理", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object query(HttpServletRequest request) {
		Map<String, Object> param = WebUtil.getParameter(request);
        ModelMap modelMap = new ModelMap();
		return super.query(modelMap, param);
	}

	@RequiresPermissions("user.read")
	@PutMapping(value = "/read/detail")
	@ApiOperation(value = "用户管理详情", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object get(HttpServletRequest request) {
		User param = WebUtil.getParameter(request, User.class);
        ModelMap modelMap = new ModelMap();
		return super.get(modelMap, param);
	}

	@PostMapping
	@RequiresPermissions("user.update")
	@ApiOperation(value = "修改用户管理", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object update(HttpServletRequest request) {
		User param = WebUtil.getParameter(request, User.class);
        ModelMap modelMap = new ModelMap();
		return super.update(modelMap, param);
	}

	@DeleteMapping
	@RequiresPermissions("user.delete")
	@ApiOperation(value = "删除用户信息", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object delete(HttpServletRequest request) {
		User param = WebUtil.getParameter(request, User.class);
        ModelMap modelMap = new ModelMap();
		return super.delete(modelMap, param);
	}
	
	@ApiOperation(value = "修改个人信息")
	@PostMapping(value = "/update/person")
	public Object updatePerson(ModelMap modelMap, @RequestBody User param) {
		param.setId(WebUtil.getCurrentUser());
		param.setPassword(null);
		Assert.isNotBlank(param.getAccount(), "ACCOUNT");
		Assert.length(param.getAccount(), 3, 15, "ACCOUNT");
		return super.update(modelMap, param);
	}
	
	// 当前用户
	@ApiOperation(value = "当前用户信息")
	@GetMapping(value = "/read/promission")
	public Object promission(ModelMap modelMap) {
		Long id = getCurrUser();
		User user = ((UserService) service).queryById(id);
		modelMap.put("user", user);
		//List<?> menus = sysAuthorizeService.queryAuthorizeByUserId(id);
		//modelMap.put("menus", menus);
		return setSuccessModelMap(modelMap);
	}
}
